<?php


namespace App\Http\Model\Console;


use App\Enum\LockHistoryEnum;
use App\Http\Library\Paas;
use Illuminate\Support\Facades\DB;

class DwModel
{

    /**
     * 查询当前表数据处理状态
     *
     * @param $data
     * @param $db
     *
     * @return bool
     */
    public static function status($data, $db)
    {
        /*查询是否有高于当前任务优先级的任务未处理完成*/
        $last = DB::select('SELECT `id`,`ctime`,`status` FROM `dw_lock_history` WHERE `data_date`=:data_date AND `db`=:db AND `is_del`=0 ORDER BY `id` DESC LIMIT 1', [
            'data_date' => $data->data_date,
            'db'        => $db,
        ]);
        /*检查上一个任务是否未处理完成*/
        if (!empty($last) && in_array($last[0]->status, [LockHistoryEnum::STATUS_BEGIN, LockHistoryEnum::STATUS_ING])) {
            /*检查任务等待时间是否过长*/
            if (strtotime($last[0]->ctime) < strtotime('-1 hour')) {
                log_arr([
                    '[数仓中间库取数]',
                    '任务阻塞',
                    'data' => $data,
                    'db'   => $db
                ], 'dw');
                Paas::yach([
                    '[数仓中间库取数]',
                    '任务阻塞',
                    'data' => $data,
                    'db'   => $db
                ]);
            }
            return false;
        }
        /*当前任务是否已经处理*/
        $exist = DB::select('SELECT * FROM `dw_lock_history` WHERE `data_date`=:data_date AND `db`=:db AND `table_name`=:table_name AND `is_del`=0 LIMIT 1', [
            'data_date'    => $data->data_date,
            'db'         => $db,
            'table_name' => $data->table_name,
        ]);
        if (empty($exist)) {
            DB::table('dw_lock_history')->insert([
                'lock_id'    => $data->id,
                'data_date'  => $data->data_date,
                'db'         => $db,
                'table_name' => $data->table_name,
                'sort'       => $data->sort,
                'status'     => LockHistoryEnum::STATUS_BEGIN
            ]);
            return true;
        }
        return false;
    }


    /**
     * 查询数仓已经处理完毕,可以使用的表
     *
     * @param string $db
     *
     * @return array
     */
    public static function list(string $db, $maxDate)
    {
        return DB::connection($db)
                 ->select('SELECT `id`,`table_name`,`data_date`,`sort` FROM `dwd_fin_capital_supervision_status_lock_fd` WHERE `data_date`>=:data_date AND `status`=:status ORDER BY `sort` ASC', [
                     'data_date' => $maxDate,
                     'status'    => 1,
                 ]);
    }


    /**
     * 需要处理的业务线-分校数据库
     *
     * @return array
     */
    public static function listDw()
    {
        return DB::select('SELECT `id`,`db`,`desc` FROM `dw_db` WHERE `is_del`=0');
    }


    /**
     * 获取处理表记录中的最大值
     *
     * @return false|string
     */
    public static function maxDate()
    {
        $data = DB::select('SELECT `data_date` FROM `dw_lock_history` WHERE `id`=(SELECT MAX(`id`) FROM `dw_lock_history`)');
        return empty($data) ? date('Y-m-d') : current($data)->data_date;
    }

}





/**
 * DwModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/4 16:42:00   操作:创建
 **/
